package com.sharing.service;

import com.alibaba.druid.sql.dialect.oracle.ast.OracleDataTypeIntervalYear;
import com.sharing.common.Page;
import com.sharing.common.Result;
import com.sharing.domain.User;
import com.sharing.domain.UserLike;
import com.sharing.mapper.UserLikeMapper;
import com.sharing.mapper.UserMapper;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;
import java.util.List;

/**
 * @author MingTian
 * @version 1.0
 * @description TODO
 * @date 2023/4/3 21:15
 */
@Service
public class UserLikeService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserLikeMapper userLikeMapper;

    /**
     * 查询指定用户所关注的所有用户的业务层方法
     * @param userId
     * @param page
     * @param size
     * @return
     */
    public Result<Page<User>> selectLikeUsersByUserId(Integer userId, Integer page, Integer size) {
        // 校验参数userId是否存在
        User user = userMapper.selectUserById(userId);
        if (user == null) {
            return new Result<>(1, "该用户id不存在");
        }

        // 从user_like表中查到用户所关注的所有用户Id
        List<Integer> ids = userLikeMapper.selectLikeUsersByUserId(userId, (page - 1) * size, size);

        // 从user_information表中查到用户的基本信息
        List<User> users = userMapper.selectUsersByUserIds(ids);

        // 查找总数
        Integer total = userLikeMapper.selectLikeUsersNumByUserId(userId);
        Page<User> page1 = new Page<>(total, size, users);

        return new Result<>(0, page1);
    }

    /**
     * 查询关注该用户的所有用户的业务层方法
     * @param userId
     * @param page
     * @param size
     * @return
     */
    public Result<Page<User>> selectUsersByLike(Integer userId, Integer page, Integer size) {
        // 校验参数userId是否存在
        User user = userMapper.selectUserById(userId);
        if (user == null) {
            return new Result<>(1, "该用户id不存在");
        }

        // 从user_like表中查到关注该用户的所有用户Id
        List<Integer> ids = userLikeMapper.selectUsersByLike(userId, (page - 1) * size, size);

        // 从user_information表中查到用户的基本信息
        List<User> users = userMapper.selectUsersByUserIds(ids);

        // 查找总数
        Integer total = userLikeMapper.selectUsersNumByLike(userId);

        Page<User> page1 = new Page<>(total, size, users);

        return new Result<>(0, page1);
    }

    /**
     * 新增关注用户的业务层方法
     * @param userLike
     * @return
     */
    public Result<Integer> insertLikeUser(UserLike userLike) {
        // 校验user_like表中是否已经存在
        Integer row = userLikeMapper.selectLikeIsHaving(userLike);

        if (row == 1) {
            return new Result<>(1, "已关注，请不要重复关注");
        }

        // 校验用户Id和被关注用户id是否存在
        if (userMapper.selectUserById(userLike.getUserId()) == null || userMapper.selectUserById(userLike.getTargetId()) == null) {
            return new Result<>(1, "用户Id不存在");
        }

        Integer row1 = userLikeMapper.insertLikeUser(userLike);

        if (row1 == 1) {
            return new Result<>(0, row1);
        }
        return new Result<>(1, "新增失败");
    }

    /**
     * 删除关注用户的业务层方法
     * @param userLike
     * @return
     */
    public Result<Integer> deleteLikeUser(UserLike userLike) {
        // 校验user_like表中是否已经存在
        Integer row = userLikeMapper.selectLikeIsHaving(userLike);

        if (row == 0) {
            return new Result<>(1, "关注关系不存在");
        }

        Integer rows = userLikeMapper.deleteLikeUser(userLike);

        if (row == 0) {
            return new Result<>(1, "删除失败");
        }

        return new Result<>(0, row);
    }
}
